package com.fescoservice.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * MiscUtil.java
 * 杂项工具类
 * 
 */
public class MiscUtil {
	
	
	public static String dateToString(String date){
		String dateStr = date.substring(6)+date.substring(0,2)+date.substring(3,5);
		return dateStr;
	}
	
	public static boolean compareDate(String beginDate,String endDate){
		String beginDateStr = dateToString(beginDate);
		String endDateStr = dateToString(endDate);
		int deta =  Integer.valueOf(beginDateStr)-Integer.valueOf(endDateStr);
		if (deta>=0){
			return true;
		}else{
			return false;
		}
	}
	
	
	/** 检查一个str是否在list<String>中出现
	 * @param str
	 * @param list
	 * @return
	 */
	public static boolean matchListValue(String str,List list) {
		if (isNullOrEmpty(list)) {
			return false;
		}
		for (int i=0;i<list.size();i++) {
			if (str.equals(list.get(i).toString())) {
				return true;
			}
		}
		return false;
	}
	
	/** 检查一个对象是否为空
	 * @param obj
	 * @return
	 */
	public static boolean isNullOrEmpty(Object obj) {
		if (obj == null)
			return true;
		
		if (obj instanceof String)
			return (obj.toString().trim()).equals("");

		if (obj instanceof CharSequence)
			return ((CharSequence) obj).length() == 0;

		if (obj instanceof Collection)
			return ((Collection) obj).isEmpty();

		if (obj instanceof Map)
			return ((Map) obj).isEmpty();

		if (obj instanceof Object[]) {
			Object[] object = (Object[]) obj;
			boolean empty = true;
			for (int i = 0; i < object.length; i++)
				if (!isNullOrEmpty(object[i])) {
					empty = false;
					break;
				}
			return empty;
		}
		return false;
	}
	
	/** 将一个对象转换成BigDecimal，如果为空则为0
	 * @param obj
	 * @return
	 */
	public static BigDecimal Object2BigDecimal(Object obj) {
		if (isNullOrEmpty(obj)) {
			return new BigDecimal(0);
		}
		if (obj instanceof BigDecimal) {
			return (BigDecimal)obj;
		}else {
			return new BigDecimal(String.valueOf(obj));
		}
	}
	
	/** 将对象转换为Integer，如果为空则为0
	 * @param obj
	 * @return
	 */
	public static Integer Object2Integer(Object obj) {
		if (isNullOrEmpty(obj)) 
			return new Integer(0);
		
		if (obj instanceof Integer) {
			return (Integer)obj;
		}else if (obj instanceof BigDecimal) {
			return ((BigDecimal)obj).intValue();
		} else {
			return Integer.parseInt(String.valueOf(obj));
		}
	}
	
	/** 将对象转换成Long，如果为空则为0
	 * @param obj
	 * @return
	 */
	public static Long Object2Long(Object obj) {
		if (isNullOrEmpty(obj)) 
			return Long.valueOf(0);
		
		if (obj instanceof Long) {
			return (Long)obj;
		}else if (obj instanceof BigDecimal) {
			return ((BigDecimal)obj).longValue();
		}else {
			return Long.parseLong(String.valueOf(obj));
		}
	}
	
	
	/** 将对象转换成Float，如果为空则为0
	 * @param obj
	 * @return
	 */
	public static Float Object2Float(Object obj) {
		if (isNullOrEmpty(obj)) 
			return new Float(0);
		
		if (obj instanceof Float) {
			return (Float)obj;
		}else if (obj instanceof BigDecimal) {
			return ((BigDecimal)obj).floatValue();
		}else {
			return Float.parseFloat(String.valueOf(obj));
		}
	}
	
	
	
	/** 将一个对象(String[]或String)转换为List<String>
	 * @param obj
	 * @return
	 */
	public static List Object2StringList(Object obj) {
		List<String> result = new ArrayList<String>();
		if (obj instanceof String[]) {
			String[] list = (String[])obj;
			for (int i=0;i<list.length;i++) {
				result.add(list[i]);
			}
		}else {
			result.add(String.valueOf(obj));
		}
		return result;
	}
	
	/** 解析一个String
	 * @param obj
	 * @return
	 */
	public static String Object2String(Object obj) {
		if (isNullOrEmpty(obj)) {
			return "";
		}
		if (obj instanceof String) {
			return (String)obj;
		}
		return String.valueOf(obj);
	}
	
	/** 将整数格式化成指定长度的二进制字符串
	 * @param value
	 * @param len
	 * @return
	 */
	public static String formatInt(int value, int len) {
		StringBuffer sb = new StringBuffer(Integer.toBinaryString(value));
		int count = len-sb.length();
		for (int i=0;i<count;i++) {
			sb.insert(0, "0");
		}
		return sb.toString();
	}
	
	/** 将long数字转换成指定长度的16进制字符串
	 * @param value
	 * @param len
	 * @return
	 */
	public static String formatLong(long value, int len) {
		StringBuffer sb = new StringBuffer(Long.toHexString(value));
		int count = len-sb.length();
		for (int i=0;i<count;i++) {
			sb.insert(0, "0");
		}
		return sb.toString();
	}
	
	/** 将16进制字符串转换成byte数组
	 * @param value
	 * @return
	 */
	public static byte[] splitColorString(String value) {
		byte[] bytes = new byte[3];
		bytes[0] = Byte.parseByte(value.substring(0, 2));
		bytes[1] = Byte.parseByte(value.substring(2, 4));
		bytes[2] = Byte.parseByte(value.substring(4, 6));
		return bytes;
	}
	
	/** 将字符串转成utf-8编码
	 * @param s
	 * @return
	 */
	public static String toUtf8String(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;
				try {
					b = Character.toString(c).getBytes("utf-8");
				} catch (Exception ex) {
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}

	/** 判断id是否在code序列(111,222,333)中出现过
	 * @param id
	 * @param code
	 * @return
	 */
	public static boolean isStringExist(String id, String code) {
		if (isNullOrEmpty(code)) return false;
		List list = StringUtil.str2List(code,",");
		for (int i=0;i<list.size();i++) {
			String c = String.valueOf(list.get(i));
			if (id.trim().equals(c.trim())) {
				return true;
			}
		}
		return false;
	}
	
	/** 判断list中是否存在idField为id的行
	 * @param list
	 * @param idField
	 * @param id
	 * @return
	 */
	public static boolean isFind(List list, String idField, String id) {
		for (int i=0;i<list.size();i++) {
			Map detailInfo = (Map)list.get(i);
			String c = String.valueOf(detailInfo.get(idField));
			if (c.equals(id)) return true; 
		}
		return false;
	}
	
	/** 根据idField从list中过滤出唯一列表
	 * @param list
	 * @param idField
	 * @return
	 */
	public static List getUniqueList(List list, String idField) {
		List<Map> result = new ArrayList<Map>();
		for (int i=0;i<list.size();i++) {
			Map detailInfo = (Map)list.get(i);
			String id = String.valueOf(detailInfo.get(idField));
			if (!isFind(result,idField, id)) {
				result.add(detailInfo);
			}
		}
		return result;
	}

	/** 从list中取得idField的值的列表
	 * @param list
	 * @param idField
	 * @return
	 */
	public static List getListSeqList(List list, String idField) {
		List<Object> result = new ArrayList<Object>();
		for (int i=0;i<list.size();i++) {
			Map info = (Map)list.get(i);
			result.add(info.get(idField));
		}
		return result;
	}

	/** 从list中取得idField的值的数组
	 * @param list
	 * @param idField
	 * @return
	 */
	public static String[] getArraySeqList(List list, String idField) {
	    List result = getListSeqList(list, idField);
	    return StringUtil.list2StringArray(result);
	}
	
	/** 从list中取得idField的值序列
	 * @param list
	 * @param idField
	 * @return
	 */
	public static String getStringSeqList(List list, String idField) {
	    List result = getListSeqList(list, idField);
	    return StringUtil.stringList2String(result);
	}
	
	/** 取得List中keyField字段为keyValue的nameField字段列表
	 * @param list
	 * @param keyField
	 * @param keyValue
	 * @param nameField
	 * @return
	 */
	public static List geStringListFromKeyValue(List list, Object keyField, Object keyValue, Object nameField) {
		String value = String.valueOf(keyValue);
		List<Object> result = new ArrayList<Object>();
		for (int i=0;i<list.size();i++) {
			Map info = (Map)list.get(i);
			String v = String.valueOf(info.get(keyField));
			if (v.equals(value)) {
				result.add(info.get(nameField));
			}
		}
		return result;
	}

	/** 取得List中keyField字段为keyValue的nameField字段序列
	 * @param list
	 * @param keyField
	 * @param keyValue
	 * @param nameField
	 * @return
	 */
	public static String geStringSeqFromKeyValue(List list, Object keyField, Object keyValue, Object nameField) {
		List result = geStringListFromKeyValue(list,keyField,keyValue,nameField);
	    return StringUtil.stringList2String(result);
	}
	
	/** 将List中的数据按keyField汇总amountField的值
	 * @param list
	 * @param keyField
	 * @param amountField
	 * @return
	 */
	public static List<Map> sumAmountList(List list, String[] keyField, String amountField) {
		List<Map> result = getUniqueAmountList(list,keyField);
		for (int i=0;i<result.size();i++) {
			Map dataInfo = (Map)result.get(i);
			BigDecimal sum = sumAmountData(list, dataInfo, keyField, amountField);
			dataInfo.put(amountField, sum);
		}
		return result;
	}

	/** 将金额合计
	 * @param list
	 * @param info
	 * @param keyField
	 * @param amountField
	 * @return
	 */
	private static BigDecimal sumAmountData(List list, Map info, String[] keyField, String amountField) {
		String[] infoValue = new String[keyField.length];
		for (int i=0;i<keyField.length;i++) {
			infoValue[i] = String.valueOf(info.get(keyField[i]));
		}

		BigDecimal count = new BigDecimal(0);
		for (int i=0;i<list.size();i++) {
			Map dataInfo = (Map)list.get(i);

			String[] dataValue = new String[keyField.length];
			for (int j=0;j<keyField.length;j++) {
				dataValue[j] = String.valueOf(dataInfo.get(keyField[j]));
				
			}
			BigDecimal amount = Object2BigDecimal(dataInfo.get(amountField));
			//判断是否都相等
			boolean flag = true;
			for (int j=0;j<keyField.length;j++) {
				if (!infoValue[j].equals(dataValue[j])) {
					flag=false;
				}
			}
			if (flag) {
				count = count.add(amount);
			}
		}
		return count;
	}
	
	/** 在List中根据keyField汇总出唯一列表
	 * @param list
	 * @param keyField
	 * @return
	 */
	private static List<Map> getUniqueAmountList(List list, String[] keyField) {
		List<Map> result = new ArrayList<Map>();
		for (int i=0;i<list.size();i++) {
			Map info = new HashMap();
			info.putAll((Map)list.get(i));
			if (!isAmountExist(info,result,keyField)) {
				result.add(info);
			}
		}
		return result;
	}

	/** 判断info在result中是否存在
	 * @param info
	 * @param result
	 * @param keyField
	 * @return
	 */
	private static boolean isAmountExist(Map info, List result, String[] keyField) {
		String[] infoValue = new String[keyField.length];
		for (int i=0;i<keyField.length;i++) {
			infoValue[i] = String.valueOf(info.get(keyField[i]));
		}
		for (int i=0;i<result.size();i++) {
			Map dataInfo = (Map)result.get(i);
			String[] dataValue = new String[keyField.length];
			for (int j=0;j<keyField.length;j++) {
				dataValue[j] = String.valueOf(dataInfo.get(keyField[j]));
				
			}
			//判断是否都相等
			boolean flag = true;
			for (int j=0;j<keyField.length;j++) {
				if (!infoValue[j].equals(dataValue[j])) {
					flag=false;
				}
			}
			if (flag) {
				return true;
			}
		}
		return false;
	}

	public static void main(String args[]) {
		
		
	}

}
